package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserActivateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class ApUserActivateServiceImpl implements ApUserActivateService {
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    ApUserMapper apUserMapper;

    @Override
    public ResponseResult activate(String uuid) {
        if (uuid == null){
            CustException.cust(AppHttpCodeEnum.NEED_LOGIN);
        }
        //通过redis  key找id
        String id = redisTemplate.opsForValue().get(uuid);
        if (id == null){
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据id查询
        ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getId, Integer.parseInt(id)));
        //判断id是否存在
        if (apUser == null){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //状态为flase  不允许操作
        if (!apUser.getStatus()){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //set  修改状态为false
        apUser.setStatus(false);

        //更新数据
        apUserMapper.updateById(apUser);
        return ResponseResult.okResult("用户激活成功");
    }
}
